import Vue from "vue";
import {actions} from '../base';
import md5 from 'js-md5';

export default {
    namespaced: true,
    state: {
        all: [],
        options: {},
        request: {
            all: false,
        },
		urls: uni.getStorageSync("download.urls") || [],
		lists: uni.getStorageSync("download.lists") || [],
		// downloads: [],
    },
    mutations: {
        SET_ALL(state, data) {
            state.all = data;
        },
        SET_REQUESTING(state, name) {
            state.request[name] = true;
        },
        SET_REQUESTED(state, name) {
            state.request[name] = false;
        },
		LISTS_ADD(state, obj) {
			state.lists.unshift(obj);
			uni.setStorageSync("download.lists", state.lists);
			
			// 同步URL
			state.urls.push(obj.url);
			uni.setStorageSync("download.urls", state.urls);
		},
		LISTS_SAVE(state, {index, data}) {
			state.lists[index] = data;
			uni.setStorageSync("download.lists", state.lists);
		},
		LISTS_DEL(state, index) {
			let row = state.lists[index];
			state.lists.splice(index, 1);
			uni.setStorageSync("download.lists", state.lists);
			
			// 同步URL
			let urlIndex = state.urls.indexOf(row.url);
			if (urlIndex > -1) {
				state.urls.splice(urlIndex, 1);
				uni.setStorageSync("download.urls", state.urls);
			}
		},
		// DOWNLOAD_ADD(state, obj) {
		// 	state.downloads.unshift(obj);
		// 	uni.setStorageSync("download.downloads", state.downloads.map(v => {
		// 		delete v.download;
		// 		return v;
		// 	}));
		// },
		// DOWNLOAD_DEL(state, index) {
		// 	let row = state.downloads[index];
		// 	state.downloads.splice(index, 1);
		// 	uni.setStorageSync("download.downloads", state.downloads);
		// },
	
		CLEAR(state) {
			state.lists = [];
			state.urls = [];
			// state.dwonlaods = [];
			uni.setStorageSync("download.lists", []);
			uni.setStorageSync("download.urls", []);
			// uni.setStorageSync("download.dwonlaods", []);
		}
    },
    actions: {
		// inLists({state}, url) {
		// 	return Object.keys().includes(md5(url));
		// }
	},
	getters: {
		// inUrls: (state) => (url) => {
		// 	return state.urls.includes(url);
		// }
	}
};
